<template>
  <div class="root">
    <van-nav-bar :fixed="true" title="发布房源" left-arrow @click-left="$router.go(-1)"/>
    <van-cell-group>
      <van-field
        :readonly="true"
        v-model="community"
        label="小区名称"
        placeholder="请输入小区名称"
        @click="$router.push('/rent/search')"
        input-align="right"
        right-icon="arrow"
      />
    </van-cell-group>
    <van-cell-group>
      <van-field v-model="price" label="租金" placeholder="请输入租金 ￥/月 " input-align="right">
        <div slot="right-icon">￥/月</div>
      </van-field>
    </van-cell-group>
    <van-cell-group>
      <van-field v-model="size" label="面积" placeholder="请输入面积 ㎡" input-align="right">
        <div slot="right-icon">㎡</div>
      </van-field>
    </van-cell-group>
    <van-field
      input-align="right"
      readonly
      clickable
      label="户型"
      :value="roomTypeLabel"
      placeholder="请选择>"
      @click="select('roomType')"
    />
    <van-field
      input-align="right"
      readonly
      clickable
      label="所在楼层"
      :value="floorLabel"
      placeholder="请选择>"
      @click="select('floor')"
    />
    <van-field
      input-align="right"
      readonly
      clickable
      label="朝向"
      :value="orientedLabel"
      placeholder="请选择>"
      @click="select('oriented')"
    />
    <van-popup v-model="showPicker" round position="bottom">
      <van-picker
        value-key="label"
        show-toolbar
        :columns="columns"
        @cancel="showPicker = false"
        @confirm="onConfirm"
      />
    </van-popup>
    <div class="title">房屋标题</div>
    <van-field v-model="title" placeholder="请输入标题 "/>
    <div class="title img">房屋图像</div>
    <van-uploader v-model="fileList" multiple/>
    <div class="title">房屋配置</div>
    <HousePackage @abc="selectSupporting"/>
    <div class="title">房屋描述</div>
    <van-field v-model="description" placeholder="请输入房屋描述信息 " rows="5" autosize type="textarea"/>
    <div class="fixed">
      <div class="left" @click="left">取消</div>
      <div class="right" @click="right">提交</div>
    </div>
  </div>
</template>

<script>
import HouseItem from "../components/HouseItem";
import HousePackage from "../components/HousePackage";
import { request, url } from "../utils/index.js";
export default {
  components: { HouseItem, HousePackage },

  async created() {
    const res = await request.get("/houses/params");
    this.query = res.data.body;
  },
  methods: {
    select(item) {
      this.type = item;
      this.columns = this.query[item];
      this.showPicker = true;
    },
    onConfirm(value, index) {
      console.log(value);
      this[this.type] = value.value;
      this[this.type + "Label"] = value.label;
      this.showPicker = false;
    },
    selectSupporting(item) {
      console.log(item);
      this.supporting = item.join("|");
    },
    left() {
      this.$router.go(-1);
    },
    async right() {
      if (this.fileList.length > 0) {
        const file = new FormData();
        this.fileList.forEach(files => {
          file.append("file", files.file);
        });
        const res = await request.post("/houses/image", file, {
          "Content-Type": "multipart/form-data"
        });
        this.houseImg = res.data.body.join("|");
      }
      const res = await request.post("/user/houses", {
        title: this.title,
        description: this.description,
        houseImg: this.houseImg,
        oriented: this.oriented,
        supporting: this.supporting,
        price: this.price,
        roomType: this.roomType,
        size: this.size,
        floor: this.floor,
        community: this.$route.query.item.value
      });
      if (res.data.status === 200) {
        this.$toast.success("发布成功");
        setTimeout(() => {
          this.$router.go(-2);
        }, 500);
      } else {
        this.$toast.success("服务器繁忙，请稍后重试");
      }
    }
  },
  data() {
    return {
      showPicker: false,
      title: "",
      description: "",
      houseImg: "img1|im2|img3",
      supporting: "空调|洗衣机",
      price: "1234",
      size: "123",
      floor: "",
      floorLabel: "",
      roomType: "",
      roomTypeLabel: "",
      oriented: "",
      orientedLabel: "",
      community:
        this.$route.query && this.$route.query.item
          ? this.$route.query.item.communityName
          : "",
      query: null,
      columns: null,
      type: undefined,
      fileList: []
    };
  }
};
</script>
<style lang="less" scoped>
.root {
  padding-top: 46px;
  padding-left: 10px;
  padding-bottom: 46px;
}
.title {
  height: 40px;
  line-height: 40px;
  border-bottom: 1px solid #cccccc;
  margin-left: -10px;
  padding-left: 10px;
}
.img {
  margin-bottom: 10px;
}
.fixed {
  background: #ffffff;
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 46px;
  display: flex;
  div {
    width: 50%;
    line-height: 46px;
    text-align: center;
  }
  .left {
    color: #21b67a;
  }
  .right {
    background: #21b67a;
    color: #ffffff;
  }
}
</style>

